package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.JwtHelper;
import com.atguigu.yygh.enums.AuthStatusEnum;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-11-11
 */
@RestController
@RequestMapping("/user/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * @Description 用户登录
     * @param loginVo:
     * @return: com.atguigu.yygh.common.result.R
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        Map<String,Object> info = userInfoService.login(loginVo);
        return R.ok().data(info);
    }

    @PostMapping("/auth/userAuth")
    public R userAuth(@RequestBody UserAuthVo userAuthVo,HttpServletRequest request){//@RequestHeader String token
        //获取当前用户的id
        Long userId = JwtHelper.getUserId(request.getHeader("token"));
        userInfoService.userAuth(userAuthVo,userId);
        return R.ok();
    }

    /**
     * @Description 获取用户信息
     * @param request:
     * @return: com.atguigu.yygh.common.result.R
     */
    @GetMapping("/auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request){//@RequestHeader String token
        UserInfo userInfo = userInfoService.getById(JwtHelper.getUserId(request.getHeader("token")));
        Map<String, Object> param;
        if (userInfo != null){
            param = userInfo.getParam();
            switch (userInfo.getAuthStatus()){
                case -1 :
                    param.put("authStatusString", AuthStatusEnum.AUTH_FAIL.getName());
                    break;
                case 0 :
                    param.put("authStatusString",AuthStatusEnum.NO_AUTH.getName());
                    break;
                case 1 :
                    param.put("authStatusString",AuthStatusEnum.AUTH_RUN.getName());
                    break;
                case 2 :
                    param.put("authStatusString",AuthStatusEnum.AUTH_SUCCESS.getName());
            }
            userInfo.setParam(param);
        }

        return R.ok().data("userInfo",userInfo);
    }
}

